package com.example.universitylibraryback.pipeline.filter;

import com.example.universitylibraryback.pipeline.Filter;
import com.example.universitylibraryback.pipeline.ReaderRegistrationContext;
import java.util.concurrent.BlockingQueue;

/**
 * 姓名标准化过滤器
 * 负责对读者姓名进行标准化处理
 */
public class NameNormalizationFilter implements Filter<ReaderRegistrationContext>, Runnable {
    private BlockingQueue<ReaderRegistrationContext> inputQueue;
    private BlockingQueue<ReaderRegistrationContext> outputQueue;

    @Override
    public void process(BlockingQueue<ReaderRegistrationContext> inputQueue,
                        BlockingQueue<ReaderRegistrationContext> outputQueue) throws InterruptedException {
        this.inputQueue = inputQueue;
        this.outputQueue = outputQueue;
        new Thread(this).start();
    }

    @Override
    public void run() {
        try {
            while (!Thread.currentThread().isInterrupted()) {
                ReaderRegistrationContext context = inputQueue.take();
                if (!context.isValid()) {
                    outputQueue.put(context);
                    continue;
                }
                // 对姓名进行标准化处理
                String normalizedName = context.getReaderName().trim().toUpperCase();
                context.setReaderName(normalizedName);
                context.setMessage(context.getMessage() + ". Name normalized.");
                outputQueue.put(context);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("NameNormalizationFilter interrupted.");
        }
    }
}